﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Loaf.自定义控件.Controls
{
    /// <summary>
    /// 修改其Content会直接触发滚动事件，默认滚动10秒，通过DisplayTime改变滚动时间
    /// </summary>
    public class TextDisplay : ContentControl
    {
        public static readonly DependencyProperty DisplayTimeProperty = DependencyProperty.Register(nameof(DisplayTime), typeof(int), typeof(TextDisplay), new PropertyMetadata(-1));

        /// <summary>
        /// 展示的时间,默认为10秒
        /// </summary>
        public int DisplayTime { get => (int)GetValue(DisplayTimeProperty); set => SetValue(DisplayTimeProperty, value); }
        /// <summary>
        /// 是否滚动
        /// </summary>
        private bool IsRunning { get; set; }
        /// <summary>
        /// 控制滚动的偏移量，主要修改translate.X
        /// </summary>
        private TranslateTransform? Translate { get; set; }

        /// <summary>
        /// 内容改变事件
        /// </summary>
        /// <param name="oldContent"></param>
        /// <param name="newContent"></param>
        protected override async void OnContentChanged(object oldContent, object newContent)
        {
            var control = (FrameworkElement)newContent;
            control.HorizontalAlignment = HorizontalAlignment.Left;
            control.VerticalAlignment = VerticalAlignment.Center;

            Translate = new TranslateTransform();
            control.RenderTransform = Translate;

            base.OnContentChanged(oldContent, control);


            await Task.Run(async () =>
            {
                while (true)
                {
                    if (control.ActualWidth != 0)
                        break;

                    await Task.Delay(30);
                }
            });
            Start();
        }

        /// <summary>
        /// 开始滚动
        /// </summary>
        public async void Start()
        {
            if (Content == null || Translate == null)
                return;

            if (DisplayTime < 0)
            {
                DisplayTime = 10;
            }

            Translate.X = this.ActualWidth;
            var control = (FrameworkElement)this.Content;
            double len = control.ActualWidth;
            IsRunning = true;
            _ = Task.Run(async () =>
            {
                while (IsRunning)
                {
                    await App.Current.Dispatcher.BeginInvoke(() =>
                    {
                        Translate.X -= 2;
                        if (Translate.X <= -len)
                        {
                            Translate.X = this.ActualWidth;
                        }
                    });
                    await Task.Delay(30);
                }
            });

            await Task.Delay(TimeSpan.FromSeconds(DisplayTime));
            Stop();
        }

        /// <summary>
        /// 停止滚动
        /// </summary>
        public void Stop()
        {
            if (Content == null || Translate == null)
                return;

            IsRunning = false;
            Translate.X = 0;
        }
    }
}
